package cn.online.qa.ctrl;

import cn.online.qa.entity.AjaxResult;
import cn.online.qa.entity.Answers;
import cn.online.qa.service.AnswersService;
import cn.online.qa.service.QuestionsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class AnswersController {
    @Resource
    AnswersService answersService;

    @Resource
    QuestionsService questionsService;
    //获得该问题的所有答案
    @RequestMapping(value = "getAnswers",produces = "application/json;charset=utf-8")
    public AjaxResult getAnswers(@RequestParam("pid") long pid){
        List<Answers> list=  answersService.getAnswers(pid);
        if(list!=null){
            return AjaxResult.success("true",list);
        }
        return AjaxResult.error("false");
    }

    //增加答案
    @RequestMapping(value = "addanswer",produces = "application/json;charset=utf-8")
    public AjaxResult answersDao(@RequestParam("qid") long qid, @RequestParam("ansContent") String ansContent, @RequestParam("answerCount") int answerCount, @RequestParam("id") long id){
        Answers answers=new Answers();
//        answers.setAnsDate(new Date());
//        answers.setAnsContent(ansContent);
        answers.setAnsdate(new Date());
       answers.setAnscontent(ansContent);
        answers.setQid(qid);
        int b=questionsService.addanswerCount(id,answerCount+1);//同步次数
        int a=  answersService.addAnswer(answers);
        return a>0? AjaxResult.success("true"): AjaxResult.error("false");
    }
    //增加次数 addanswerCount
//    @RequestMapping("addanswerCount")
//    public AjaxResult addanswerCount(@RequestParam("id") long id,@RequestParam("answerCount") int answerCount){
//        int a=questionsService.addanswerCount(id,answerCount);
//        return a>0?AjaxResult.success("true"):AjaxResult.error("false");
//    }

}
